using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.CompanyConstant
{
    public partial class guiCurrency : THSERPScreen
    {
        //List<GECurrenciesInfo> lstGECurrencies = new List<GECurrenciesInfo>();
        DataSet dsCurrencies = new DataSet();
        public guiCurrency()
        {
            InitializeComponent();
        }

        private void guiCurrency_Load(object sender, EventArgs e)
        {
            //InitCurrencyToList();            
            GECurrenciesController objGECurrenciesController = new GECurrenciesController();
            dsCurrencies = objGECurrenciesController.GetAllObjects();            
            fld_dgcCurrency.DataSource = dsCurrencies.Tables[0];
        }

        private void fld_dgvCurrency_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DataRow newRow = fld_dgvCurrency.GetDataRow(e.RowHandle);
            newRow["GECurrencyID"] = 0;
            newRow["GECurrencyDateFrom"]=DateTime.Now;
        }

        private void fld_dgvCurrency_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                if (e.FocusedRowHandle < 0)
                {
                    colGECurrencyName.OptionsColumn.AllowEdit = true;
                }
                else
                {
                    int iGECurrencyID = (int)fld_dgvCurrency.GetRowCellValue(e.FocusedRowHandle, colGECurrencyID);
                    if (iGECurrencyID > 0)
                        colGECurrencyName.OptionsColumn.AllowEdit = false;
                    else
                        colGECurrencyName.OptionsColumn.AllowEdit = true;
                }
            }
            catch (Exception)
            {
                return;
            }

        }


        public void SaveCurrency()
        {
            GECurrenciesController objGECurrenciesController = new GECurrenciesController();
            foreach (DataRow rowCurrency in dsCurrencies.Tables[0].Rows)
            {
                GECurrenciesInfo objGECurrenciesInfo = (GECurrenciesInfo)objGECurrenciesController.GetObjectFromDataRow(rowCurrency);
                int iGECurrency = (int)rowCurrency["GECurrencyID"];
                if (iGECurrency > 0)
                {                    
                    objGECurrenciesController.UpdateObject(objGECurrenciesInfo);
                }
                else
                {
                    rowCurrency["GECurrencyID"]=objGECurrenciesController.CreateObject(objGECurrenciesInfo);
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            SaveCurrency();
            MessageBox.Show("Gespeichert", "#Message#");
        }

    
        
    }
}